package org.NooLab.glue.subscription.publisher;

import java.util.Arrays;

import org.NooLab.compare.utilities.math.DistanceControlProperties;
import org.NooLab.glue.instances.ParticipantBaseIntf;
import org.NooLab.glue.instances.ParticipantFactory;
import org.NooLab.glue.subscription.FutureIntf;
import org.NooLab.glue.subscription.FuturesIntf;
import org.NooLab.utilities.logging.PrintLog;

public class SubscriptionPublisher {

	FuturesIntf futures ;
	ParticipantBaseIntf participant;
	DistanceControlProperties distanceCtrl;
	ParticipantFactory factory;
	
	String label;
	long InstanceID= -1;
	
	String[] featuredItems, excludingItems;
	double[] featureValues;
	
	double minimalMatchThreshold=0.01, minimalSimThreshold=0.1;
	// 
	
	PrintLog out = new PrintLog (2,false);
	
	public SubscriptionPublisher( ParticipantFactory factory ,// ParticipantBaseIntf participant, 
								  FuturesIntf futures ){
		
		this.factory = factory;
		ParticipantBaseIntf part = (ParticipantBaseIntf) (factory.getInstance()); 
		 
		this.participant = part;
		// ??? futures in participant
				
				
		this.futures = futures;
		if (participant.getFutures()==null){
			participant.setFutures(futures) ;
		}
		distanceCtrl = factory.getDistanceCtrlProperties() ;
		
	
	}

	
	public String publishPreparedIssueItemset() throws Exception{
		String subguid;
		int r;
		
		int domain = DistanceControlProperties._DD_SIMPLESET;
		FutureIntf subscription;

		// we do not create the subscription like this... FutureIntf subscription = futures.create(distanceCtrl);
		// instead we request an instance of a subscription from the participant object 
		// (which is the "natural" owner of a subscription)
		subscription = participant.createFuture();
		
		subscription.addProperty( "keywords",       // label 
				                  featuredItems,  	// items of the defining set
				                  excludingItems,   // excluding items, any contained in defining set will be removed
				                  minimalMatchThreshold) ; // minimal match threshold ... >0.33 <0.66 -> at least 1 match
		
		// if we want to use other than default settings for similarity (=distance) calculations,
		//  ... we even could to create a new instance (param=1), 
			distanceCtrl = factory.getDistanceCtrlProperties() ;
		// then set the field values as desired ...
			distanceCtrl.setItemSetContrastalpha(0.2) ;
			distanceCtrl.setItemSetContrastbeta(0.4) ;
			
		// ... and provide it to the subscription, only if it was a new instance
		//	subscription.setDistanceControlProperties(distanceCtrl) ;
			
			
		subguid = futures.add( subscription ); // create an object from the data
		
		// ----------------------------------------------------------------------------------------------------

												out.print(2, "going to publish the subscription... ");
		r = futures.publish( subguid );
		// FXMessage{} is used to create the xml representation
		// publishing within MessageBoard is actually "performed" by "SubscriptionHandlerInterpreter{}"
		
			if (r<0){
				throw( new Exception("publishing the simple subscription failed! (r="+r+")\n" ) ) ;
			}else{
				out.print(2, "simple subscription "+subguid+" has been published.");
			}
		return subguid;
	}
	
	public String publishPreparedIssueFeatureVector() throws Exception{
		
		String subguid;
		int r;
		
		 
		FutureIntf subscription;

		// we do not create the subscription like this... FutureIntf subscription = futures.create(distanceCtrl);
		// instead we request an instance of a subscription from the participant object 
		// (which is the "natural" owner of a subscription)
		
		subscription = participant.createFuture();  // create a subscription object 
			
		subguid = futures.add( subscription );      // ... and a slot for it
		
		
		subscription.addProperty( "profile X",      // label 
								  featuredItems,    // feature items (fields) of the vector
								  featureValues,    // the numerical values
								  excludingItems,   // optional: excluding features, suffix-wildcards are allowed here!
								  					// overlaps with feature set will be removed	
								  minimalMatchThreshold,
								  minimalSimThreshold ) ; // that's (1) set matching threshold, (2) a relative value for  minimal similarity threshold of remaining profile
			
		            
		// ----------------------------------------------------------------------------------------------------

												out.print(2, "going to publish the subscription... ");
		r = futures.publish( subguid );
		// FXMessage{} is used to create the xml representation
		// publishing within MessageBoard is actually "performed" by "SubscriptionHandlerInterpreter{}"
		
			if (r<0){
				subguid = "";
				throw( new Exception("publishing the simple subscription failed! (r="+r+")\n" ) ) ;
			}else{
				out.print(2, "simple subscription "+subguid+" has been published.");
			}
		
		return subguid ;
	}
	
	
	public DistanceControlProperties getDistanceCtrl() {
		return getDistanceCtrl(0);
	}
	public DistanceControlProperties getDistanceCtrl( int mode ) {
		
		if (mode>=1){
			distanceCtrl = factory.getDistanceCtrlProperties(1) ;
		}
		
		return distanceCtrl;
	}
	
	public void setInstanceID( long iid) {
		InstanceID = iid;
	}
	
	public void setItemsOfFeatureSet( String[] items){
		featuredItems = Arrays.copyOf(items , items.length) ;
		 
	}

	public void setFeatureValues(double[] fvalues) {
		this.featureValues = fvalues; // Arrays.copyOf(fvalues,fvalues.length);
	}


	public void setExcludingItems(String[] exitems) {
		this.excludingItems = exitems; // Arrays.copyOf(exitems,exitems.length);
	}


	public void setSubscriptionLabel(String label) {
		 this.label = label;
		
	}

	public void setMinimalMatchThreshold(double cvalue) {
		 
		minimalMatchThreshold = cvalue;
	}

	public void setMinimalSimilarityThreshold(double cvalue) {
	 
		minimalSimThreshold = cvalue;
	}
 	
}
